package com.nCoV.EpidemicSurveillance.repository;

import java.util.List;
import java.util.Map;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Component;

import com.nCoV.EpidemicSurveillance.domain.AreaReach;
import com.nCoV.EpidemicSurveillance.domain.CxArea;
import com.nCoV.EpidemicSurveillance.domain.PatientAreaRelation;
import com.nCoV.EpidemicSurveillance.domain.PlaceNodes;

/**
 * @author 罗穆健
 * @date 2020年3月31日
 */
@Component
public interface AreaRepository extends Neo4jRepository<PlaceNodes, Long>{

	@Query("Merge (area:AreaReach{addressName::#{#addressName},xqAddress::#{#xqAddress}})")
	void addNewArea(String addressName,String xqAddress);

	@Query("match(pat:Person{idCard::#{#patientIdCard}}) "
			+ "match(area:AreaReach{xqAddress::#{#areaReach.xqAddress},addressName::#{#areaReach.addressName}}) "
			+ "merge (pat)-[r:ReachRelation]->(area)  "
			+ "set r.contactName=:#{#patientAreaRelation.contactName}"
			+ "set r.reachTime=:#{#patientAreaRelation.reachTime}"
			+ "set r.leaveTime=:#{#patientAreaRelation.leaveTime}"
			+ "set r.kzqk=:#{#patientAreaRelation.kzqk}"
			+ "set r.tfqk=:#{#patientAreaRelation.tfqk}")
	void createPatientAreaRealtion(AreaReach areaReach, PatientAreaRelation patientAreaRelation, String patientIdCard);

	@Query("match(n:Person{idCard::#{#patientIdCard}})-[R:ReachRelation]->(m:AreaReach) "
			+ "where m.addressName =~:#{#searchAreaData} "
			+ "and R.contactName in ['reach'] "
			+ "return R.reachTime as reachTime, "
			+ "R.leaveTime as leaveTime, "
			+ "R.contactName as contactName,"
			+ "m.addressName as addressName,"
			+ "m.xqAddress as xqAddress, "
			+ "R.kzqk as kzqk,"
			+ "R.tfqk as tfqk,"
			+ "id(m) as areaId,"
			+ "id(R) as relationId "
			+ "skip :#{#skipNum} "
			+ "limit :#{#limitNum}")
	List<Map<String, Object>> queryAreaByRelation(String patientIdCard, String searchAreaData, int skipNum, int limitNum);

	@Query("match(n:Person{idCard::#{#patientIdCard}})-[R:ReachRelation]->(m:AreaReach) "
			+ "where m.addressName =~:#{#searchAreaData} "
			+ "and R.contactName in ['reach'] "
			+ "return count(*) as count")
	int getAreaRelationCount(String patientIdCard, String searchAreaData);

	@Query("match (p:Person{idCard::#{#patientIdCard}}) - "
			+ "[R:ReachRelation{contactName::#{#contactName}}] ->"
			+ "(a:AreaReach{addressName::#{#addressName},xqAddress::#{#xqAddress}}) delete R")
	void deleteAreaRelationByIdCard(String patientIdCard,
			String contactName, String addressName,String xqAddress);


	@Query("match ()-[n]->(m) where id(n) = :#{#relationId} and id(m)=:#{#areaId} "
			+ "set n.reachTime=:#{#reachTime} "
			+ "set n.leaveTime=:#{#leaveTime} "
			+ "set m.addressName=:#{#addressName} "
			+ "set m.xqAddress=:#{#xqAddress} "
			+ "set n.kzqk=:#{#kzqk} "
			+ "set n.tfqk=:#{#tfqk}")
	void updateAreaRelation(Long areaId, Long relationId, String reachTime, String leaveTime, String addressName, String xqAddress, String tfqk, String kzqk);

	@Query("match (n)-[r:ReachRelation]->(m:AreaReach) where m.addressName =~ :#{#address} and n.grgl ='1.0' "
			+ "return m.addressName as addressName,"
			+ "m.xqAddress as xqAddress, r.reachTime as reachTime,r.leaveTime as leaveTime")
	List<Map<String, Object>> getAreaData(String address);
}
